import { observable, action, computed } from "mobx";
import { each } from 'lodash';
import API from '../../../apis/task';
import { TaskViewType, TaskViewPageType } from '../../../constants/task';
import TaskViewExcelStore from "./excel";
import TaskViewCustomPageStore from "./cuspage";
import { WeaTableNew } from 'comsMobx'
import * as Actions from './action';
import FormViewStore from "../../form/ViewStore";
const { TableStore } = WeaTableNew;
import { WeaLocaleProvider } from "ecCom";
const getLabel = WeaLocaleProvider.getLabel;

export default class TaskViewStore {
    @observable selectedTab = 'page';
    @observable taskid = '';
    @observable nodeid = '';
    @observable node;
    @observable task = {};
    @observable title = '';
    @observable displayType = '';
    @observable loading = false;
    @observable noRight = false;
    @observable iscurrentnode = true;
    @observable reportType = true;
    @observable hasChild = false;

    @observable layout = '0';

    @observable excel = new TaskViewExcelStore();
    @observable form = new FormViewStore();
    @observable cuspage = new TaskViewCustomPageStore();
    @observable tableStore = new TableStore();
    @observable canSubmit = false;
    @observable canUndo = false;
    @observable uploadtitle = getLabel(502835, '导入数据');
    @observable visible = false;

    // @observable hasSaveBtn = true;
    // @observable hasDistributeBtn = true;
    // @observable hasExportBtn = true;
    // @observable hasImportBtn = true;

    constructor() {
        each(Actions, (value, key) => {
            this[key] = action(value.bind(this))
        });
    }

    @computed
    get tabs() {
        const tabs = [...TaskViewType];
        if (!this.nodeid) {
            tabs.shift();
        } else {
            if (this.displayType === TaskViewPageType.EXCEL) {
                tabs[0] = { key: 'page', title: getLabel(502895, '表格') }
            } else if (this.displayType === TaskViewPageType.FORM) {
                tabs[0] = { key: 'page', title: getLabel(502896, '表单') }
            }
        }
        return tabs;
    }

    @action onChangeTab = (selectedTab) => {
        this.selectedTab = selectedTab;
        if (this.selectedTab === 'page') {
            if (this.displayType === TaskViewPageType.EXCEL) {
                this.excel.refresh();
            }
        } else {
            this[`init${selectedTab}`]();
        }
    }

    @action init = (params) => {
        this.loading = true;
        this.params = params;
        this.taskid = params.taskid;
        this.nodeid = params.nodeid;
        if (this.nodeid) {
            this.selectedTab = 'page';
        } else {
            this.selectedTab = 'path';
        }
        API.task(params).then(this.setTaskInfo).catch(error => {
            if (error.api_errormsg === getLabel('503347', '没有权限')) {
                this.noRight = true;
                this.loading = false;
            }
        });

    }
    @action setTaskInfo = (task) => {
        if (task.noRight) {
            this.noRight = true;
            this.loading = false;
            return;
        }
        delete task.app;
        this.task = task;
        this.title = task.name;
        this.canSubmit =this.excel.canSubmit = !!task.canSubmit;
        this.canUndo = !!task.canUndo;
        this.iscurrentnode = !!task.iscurrentnode;
        this.reportType = !!task.reportType;
        this.hasChild = !!task.hasChild;
        this[`init${this.selectedTab}`]();
    }
    @action initpage = () => {
        this.loading = true;
        API.page(this.params).then(this.setPage);
    }
    @action setPage = (page) => {
        this.displayType = page.pageType;
        if (this.displayType === TaskViewPageType.FORM) {
            this.form.init(page.formPageId);
        } else if (this.displayType === TaskViewPageType.EXCEL) {
            this.excel.init({ nodeid: this.nodeid, pageid: page.hrefLinkId, dataid: page.dataId || '', form: 'task',taskid: this.taskid });
        } else if (this.displayType === TaskViewPageType.CUSTOM) {
            this.cuspage.init({ page });
        }
        this.loading = false;
    }
    @action initpath = () => {
        this.loading = true;
        API.path(this.params).then(this.setPathNode);
    }
    @action setPathNode = (node) => {
        this.node = node;
        this.loading = false;
    }
    @action initlog = () => {
        this.loading = true;
        API.log(this.params).then(this.setLog);
    }
    @action setLog = (log) => {
        this.tableStore.getDatas(log.datas);
        this.loading = false;
    }
    @action setVisible = (bool) => {
        this.visible = bool;
    }
}